% Basic Introduction:
% This function transforms a wide sparse matrix (size=[D1,D2_1*D2_2]) 
% to a wide matrix (size=[D1*D2_1,D2_2]).
% This transformation can facilitate the expectation computation 
% significantly.
% M_long=WideToLong(M_wide,[D1;D2_1;D2_2])
% Example:
% If size(A_i)=[D1,D2_2] and D2_1=2, then this function will transform
% [A_1,A_2] to [A_1;A_2].
function [M_long,Wi,Wj,Val]=SpaFun_Wide2Long(M_wide,DimInfo)

D1              =   DimInfo(1);
D2_1            =   DimInfo(2);
D2_2            =   DimInfo(3);

[Wi,Wj,Val]     =   find(M_wide);

multiple        =   ceil(Wj/D2_2)-1;

Li              =   Wi+multiple*D1;
Lj              =   Wj-multiple*D2_2;

M_long          =   sparse(Li,Lj,Val,D1*D2_1,D2_2);
